package com.csdemos.algs.table;

import java.util.LinkedList;

import com.csdemos.algs.function.Function;
import com.csdemos.algs.function.HashFunction;

public class SequenceHashTable implements HashTable{
    
    private HashFunction function;
    protected LinkedList<Integer>[] table;
    
    @SuppressWarnings("unchecked")
	public SequenceHashTable(HashFunction function){
        this.function = function;
        table = (LinkedList<Integer>[]) new LinkedList[Function.m];
    }

    @Override
    public Integer insert(int value) {
        int key = function.hash(value);
        if(table[key] != null){
            table[key].add(value);
        } else {
            table[key] = new LinkedList<Integer>();
            table[key].add(value);
        }
        return key;
    }

    @Override
    public Integer search(int key) {
        if(table[key] == null || table[key].isEmpty()){
            return null;
        }
        return table[key].getFirst();
    }

    @Override
    public boolean delete(int value) {
        int key = function.hash(value);
        if(table[key] == null || table[key].isEmpty()){
            return false;
        } else {
        	return table[key].remove(Integer.valueOf(value));
        }
    }
    
}
